package com.rcnie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rcnie.common.R;
import com.rcnie.dto.DishDto;
import com.rcnie.entity.Dish;
import com.rcnie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 负责人：Syang
     * 未完成功能：通过cookie判断是否已经登录
     * 功能：根据饭堂位置请求dish数据
     * @param request
     * @param dish
     * @return
     */
//    @PostMapping("/select")
//    public DishDto selectDish(HttpServletRequest request,@RequestBody Dish dish){
//        //获取用户id
////       int userId = (int) request.getSession().getAttribute("userId");
//        int userId=1;
//        DishDto dishDto = dishService.selectDish(userId,dish);
//        return dishDto;
//    }


    /**
     * 查找全部
     * @param
     * @param
     * @return
     */
    @PostMapping("/selectAll")
    public R<List<Dish>> selectAll(){
        List<Dish> list = dishService.list();

        R<List<Dish>> succes = R.succes(list);
        succes.setMsg("获取成功");
        return succes;
    }


    /**
     * 根据id查询
     * @param dish
     * @return
     */
    @PostMapping("/selectById")
    public R<Dish> selectById(@RequestBody Dish dish){
        Dish byId = dishService.getById(dish.getId());

        return R.succes(byId);
    }

    /**
     * 删除
     * @param
     * @param
     * @return
     */
    @PostMapping("/deleteDish")
    public R delDish(@RequestBody Dish dish){
      
        if(dishService.removeById(dish.getId())){
            List<Dish> list = dishService.list();
            R<List<Dish>> succes = R.succes(list);
            succes.setMsg("删除成功");
            return succes;
        }else {
            return R.error("删除失败");
        }
    }

    /**
     * 更新
     * @param
     * @param dish
     * @return
     */
    @PostMapping("/updateDish")
    public R<List<Dish>> updateDish(@RequestBody Dish dish){
        System.out.println(dish.getId());
        if(dishService.updateById(dish)){
            List<Dish> list = dishService.list();
            R<List<Dish>> succes = R.succes(list);
            succes.setMsg("更新成功");
            return succes;
        }else {
            return R.error("更新失败");
        }
    }

    /**
     * 增
     * @param
     * @param
     * @return
     */
    @PostMapping("/addDish")
    public R<List<Dish>> addDish(@RequestBody Dish dish){
        dish.setImage("public\\kfc.png");

        if(dishService.save(dish)){
            List<Dish> list = dishService.list();
            R<List<Dish>> succes = R.succes(list);
            succes.setMsg("新增成功");
            return succes;
        }else {
            return R.error("新增失败");
        }
    }
}
